# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  # 设置主机与虚拟机的共享目录
  #config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
  # https://stackoverflow.com/questions/22922891/vagrant-ssh-authentication-failure
  #config.ssh.private_key_path = "~/.ssh/id_rsa"
  #config.ssh.forward_agent = true
  numNodes = 3
  nodeRange = 1..3
  (nodeRange.first).upto(nodeRange.last).each do |i|
    config.vm.define "hadoop10#{i+1}" do |node|
      # 设置虚拟机的Box
      node.vm.box = "centos/7"
      # 设置虚拟机的Box的版本号
      node.vm.box_version = "1804.02"
      # 设置虚拟机的主机名:hdp101
      # node.vm.hostname="hdp10#{i}"
      node.vm.hostname="hadoop10#{i+1}"
      # 虚拟机的默认登录名和密码："vagrant","vagrant"
      #node.ssh.username = "vagrant"
      # 设置虚拟机的登录密码
      #node.ssh.password = "vagrant"
      # 是否通过公私钥来登录虚拟机，默认为true,如果为True,可能会出现一直Warning: Authentication failure. Retrying...
      node.ssh.insert_key = false
      # 设置虚拟机的IP
      node.vm.network "private_network", ip: "192.168.10.#{100+i}"

      # VirtaulBox相关配置
      node.vm.provider "virtualbox" do |v|
        # 设置虚拟机的名称
        # v.name = "hdp10#{i}"
        v.name = "hadoop10#{i+1}"
        if i == nodeRange.first
            # 设置虚拟机的内存大小
            v.memory = 6144
        else
            # 设置虚拟机的内存大小
            v.memory = 4096
        end
        # 设置虚拟机的CPU个数
        v.cpus = 1
      end

      # 使用shell脚本进行软件安装和配置
      node.vm.provision "shell",  path: "scripts/setup-centos.sh"
      node.vm.provision "shell" do |s|
        s.path = "scripts/setup-init.sh"
        s.args = "-i #{i}"
      end
      node.vm.provision "shell",  path: "scripts/setup-hosts.sh"
      node.vm.provision "shell",  path: "scripts/setup-java.sh"
      node.vm.provision "shell",  path: "scripts/setup-hadoop.sh"

      if i == nodeRange.first
        node.vm.provision "shell" do |s|
            s.path = "scripts/setup-datax.sh"
         end
        node.vm.provision "shell" do |s|
            s.path = "scripts/setup-hive.sh"
            # s.privileged = false
        end
        node.vm.provision "shell" do |s|
            s.path = "scripts/setup-dolphinscheduler.sh"
        end
        # node.vm.provision "shell" do |s|
        #    s.path = "scripts/setup-kibana.sh"
        # end
        # node.vm.provision "shell" do |s|
        #   s.path = "scripts/setup-nginx.sh"
        # end
        # node.vm.provision "shell" do |s|
        #    s.path = "scripts/setup-redis.sh"
        # end
        # node.vm.provision "shell" do |s|
        #    s.path = "scripts/setup-canal.sh"
        # end
        node.vm.provision "shell" do |s|
           s.path = "scripts/setup-maxwell.sh"
        end
        # node.vm.provision "shell" do |s|
        #   s.path = "scripts/setup-maven.sh"
        # end
        # node.vm.provision "shell" do |s|
        #   s.path = "scripts/setup-azkaban.sh"
        # end
        # node.vm.provision "shell" do |s|
        #   s.path = "scripts/setup-kylin.sh"
        # end
      end

      # node.vm.provision "shell",  path: "scripts/setup-hbase.sh"
      node.vm.provision "shell",  path: "scripts/setup-scala.sh"
      node.vm.provision "shell",  path: "scripts/setup-spark.sh"
      # node.vm.provision "shell",  path: "scripts/setup-sqoop.sh"

      # node.vm.provision "shell",  path: "scripts/setup-phoenix.sh"
      node.vm.provision "shell",  path: "scripts/setup-flume.sh"
      # node.vm.provision "shell",  path: "scripts/setup-flink.sh"


      node.vm.provision "shell" do |s|
        s.path = "scripts/setup-zookeeper.sh"
        s.args = "-i #{i}"
      end
      node.vm.provision "shell" do |s|
        s.path = "scripts/setup-kafka.sh"
        s.args = "-i #{i}"
      end
      # node.vm.provision "shell" do |s|
      #   s.path = "scripts/setup-presto.sh"
      #   s.args = "-i #{i}"
      # end
      # node.vm.provision "shell" do |s|
      #   s.path = "scripts/setup-es.sh"
      #   s.args = "-i #{i}"
      # end

      if i == nodeRange.last
        node.vm.provision "shell" do |s|
          s.path = "scripts/setup-mysql.sh"
        end
      end

      node.vm.provision "shell",  path: "scripts/setup-dataware.sh"
      # node.vm.provision "shell",  path: "scripts/setup-kerberos.sh"
      # node.vm.provision "shell",  path: "scripts/setup-zabbix.sh"
      node.vm.provision "shell",  path: "scripts/setup-clean.sh"
    end
  end
end
